package com.android.support.lib.universal.ui.guide;

import java.util.ArrayList;
import java.util.List;

import com.android.support.lib.universal.R;
import com.android.support.lib.universal.base.BaseActivity;

import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.view.View;
import android.view.View.OnClickListener;

/**
 * 引导页,背景具有滑动效果。】图形必须放在 drawable-hdpi 目录下。不然像小米等手机。会报错，无法找到资源.
 * 子类只需调用 initViewPager(int backgroundID,List<Integer> fragmentsLayoutIDs, int CenTerID) 方法即可。
 * 子类不用添加布局文件。
 */

public class GuideActivity extends BaseActivity implements OnClickListener {

    private GuideViewPager viewPager;
    private List<Fragment> fragments;

    private int CenTerID;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lib_activity_guide);
        initUI();
    }

    @Override
    protected void initUI() {
        // TODO Auto-generated method stub
        viewPager = (GuideViewPager) findViewById(R.id.guideviewpager);
        fragments = new ArrayList<Fragment>();
    }

    /**
     *
     * @param backgroundID ViewPager的背景滑动图片
     * @param fragmentsLayoutIDs fragement布局文件集合
     * @param CenTerID 最后一个fragement的点击按钮ID
     */
    protected void initViewPager(int backgroundID,List<Integer> fragmentsLayoutIDs, int CenTerID) {
        this.CenTerID = CenTerID;
        //添加可以滑动拉伸的背景图片
        viewPager.setBackGroud(BitmapFactory.decodeResource(getResources(), backgroundID));
        //多个Fragment不能同时引用同一个布局文件。一个Fragment对应一个布局文件
        for (int i = 0; i < fragmentsLayoutIDs.size(); i++) {
            fragments.add(new FragmentGuide(fragmentsLayoutIDs.get(i)));
        }
        listenner();
    }

    @Override
    protected void listenner() {
        // TODO Auto-generated method stub
        GuideViewPagerAdapter adapter = new GuideViewPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if (v.getId() == CenTerID) {
            this.finish();
        }
    }

    private class GuideViewPagerAdapter extends FragmentPagerAdapter {
        private List<Fragment> list;

        public GuideViewPagerAdapter(FragmentManager fm, List<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @Override
        public int getItemPosition(Object object) {
            return super.getItemPosition(object);
        }

        @Override
        public Fragment getItem(int arg0) {
            return list.get(arg0);
        }

        @Override
        public int getCount() {
            return list.size();
        }

    }
}

